<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\TestApi;

use Carbon\Carbon;
use App\Models\Eloquent;

/**
 * Class QtpTestParamRe
 *
 * @property int $id
 * @property string $test_report_uid
 * @property int $test_module_id
 * @property int $test_api_id
 * @property int $api_id
 * @property int $param_id
 * @property array|null $param
 * @property array|null $res
 * @property string $http_code
 * @property int $duration
 * @property int $test_validator_sum
 * @property int $success_validator_sum
 * @property int $status
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 *
 * @package App\Models
 */
class TestParamRes extends Eloquent
{
	protected $table = 'test_param_res';

	protected $casts = [
		'test_module_id' => 'int',
		'test_api_id' => 'int',
		'api_id' => 'int',
		'param_id' => 'int',
		'param' => 'json',
		'res' => 'json',
		'duration' => 'int',
		'test_validator_sum' => 'int',
		'success_validator_sum' => 'int',
		'status' => 'int'
	];

	protected $fillable = [
		'test_report_uid',
		'test_module_id',
		'test_api_id',
		'api_id',
		'param_id',
		'param',
		'res',
		'http_code',
		'duration',
		'test_validator_sum',
		'success_validator_sum',
		'status'
	];

    public static function getParamsByRptUid(string $test_report_uid)
    {
        return self::where('test_report_uid', $test_report_uid)->orderBy("id", "ASC")->get();
    }

    // 0待测试 1接口参数请求错误 2请求成功验证器未通过 3请求成功验证器也通过
    const STATUS_INIT = 0;
    const STATUS_ERR = 1;
    const STATUS_NOT_PASS = 2;
    const STATUS_PASS = 3;
    public static $status_maps = [
        self::STATUS_INIT => '待测试',
        self::STATUS_ERR => '请求错误',
        self::STATUS_NOT_PASS => '验证器未通过',
        self::STATUS_PASS => '成功通过',
    ];

    public function label()
    {
        $testReport = TestReport::getByUid($this->test_report_uid);
        $this->testModule = TestModule::find($this->test_module_id);
        $this->testAPi = TestApi::find($this->test_api_id);
        $this->test_adminer = $testReport->test_adminer ?? '';
        $this->created_adminer = $testReport->created_adminer ?? '';
        $this->status_label = self::$status_maps[$this->status] ?? '';
        $this->tested_at = $testReport->tested_at ?? '';
        return $this;
    }

    public static $fillables = [
        'test_report_uid' => '测试报告唯一UUID',
        'test_module_id' => '测试模块ID',
        'test_api_id' => '测试接口ID',
        'api_id' => '原始接口ID',
        'param_id' => '原始参数ID',
        'param' => '用例',
        'res' => '测试返回结果',
        'http_code' => '返回http状态码',
        'duration' => '用例运行时长',
        'test_validator_sum' => '验证器总数',
        'success_validator_sum' => '验证器通过总数',
        'status' => '状态'
    ];
}
